package com.example.fundappcation.Receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

import com.example.fundappcation.utils.NetWorkResult;

public class NetWorkReceiver extends BroadcastReceiver {
    private int result = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TAG", "网络发生变化");
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {//小于23
            //获取ConnectivityManager对象对应的NetworkInfo对象
            //获取WIFI连接的信息
            NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //获取移动数据连接的信息
            NetworkInfo dataNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                result = NetWorkResult.NETWORK_WIFI;
                Log.d("TAG", "WIFI已连接,移动数据已连接");
            } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
                result = NetWorkResult.NETWORK_WIFI;
                Log.d("TAG", "WIFI已连接,移动数据已断开");
            } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                result = NetWorkResult.NETWORK_WIFI;
                Log.d("TAG", "WIFI已断开,移动数据已连接");
            } else {
                result = NetWorkResult.NETWORK_MOBILE;
                Log.d("TAG", "WIFI已断开,移动数据已断开");
            }

        } else {//大于23
            Network network = connectivityManager.getActiveNetwork();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {//sdk版本大于等于29

                NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
                if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {//使用移动网络
                    Log.d("TAG", "移动网络");
                    result = NetWorkResult.NETWORK_4G;
                }
                if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {//使用wifi
                    Log.d("TAG", "wifi");
                    result = NetWorkResult.NETWORK_WIFI;
                }
                return;
            }
            //获取所有网络连接的信息
            Network[] networks = connectivityManager.getAllNetworks();
            //用于存放网络连接信息
            StringBuilder sb = new StringBuilder();
            //通过循环将网络信息逐个取出来
            for (int i = 0; i < networks.length; i++) {
                //获取ConnectivityManager对象对应的NetworkInfo对象
                NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networks[i]);
                sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected());
            }
            Log.d("TAG", sb.toString());
        }
    }
}
